package com.loyal.loyalerp.humenresource.kaoqinMonth.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.basedata.company.mapper.GyCompanyMapper;
import com.loyal.loyalerp.humenresource.askleave.mapper.AskLeaveMapper;
import com.loyal.loyalerp.humenresource.askleave.model.AskLeave;
import com.loyal.loyalerp.humenresource.kaoqinMonth.mapper.RsKaoqinmonthMapper;
import com.loyal.loyalerp.humenresource.kaoqinMonth.model.RsKaoqinmonth;
import com.loyal.loyalerp.humenresource.kaoqinMonth.model.vo.CompanyIdAndAskVO;
import com.loyal.loyalerp.humenresource.kaoqinMonth.service.KaoqinMonthService;
import com.loyal.loyalerp.mapper.GyPersonMapper;
import com.loyal.loyalerp.mapper.KaoQinMapper;
import com.loyal.loyalerp.model.GyPerson;
import com.loyal.loyalerp.model.vo.CompanyIdAndYears;

@Service
public class KaoqinMonthServiceImpl implements KaoqinMonthService {
	
	@Autowired
	RsKaoqinmonthMapper kaoqinMonthMapper;
	
	@Autowired
	GyCompanyMapper companyMapper;
	
	@Autowired
	AskLeaveMapper askLeaveMapper;
	
	@Autowired
	GyPersonMapper personMapper;
	
	@Autowired
	KaoQinMapper kaoqinMapper;

	@Override
	public PageInfo<RsKaoqinmonth> listKaoqinMonth(Integer pageSize, Integer pageNum, String sidx, String sord) {
		PageHelper.startPage(pageSize, pageNum);
		List<RsKaoqinmonth> list = kaoqinMonthMapper.listSortedKaoqinMonth(sidx, sord);
		PageInfo<RsKaoqinmonth> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	/**
	 * 加载数据：
	 * 1.得到当前年currentYear
	 * 2.得到当前月currentMonth
	 * 3.将当前月到这一年12月份每个月的月结状态(mCloseFlag)设置为0(未月结) 
	 */
	//2018-3-28:3,4,5,6,7,8,9,10,11,12(month++)
	@Override
	public int initData() {
		Calendar now = Calendar.getInstance();
		int currentYear=now.get(Calendar.YEAR);
		int currentMonth=now.get(Calendar.MONTH)+1;
		
		RsKaoqinmonth kaoqinMonth=new RsKaoqinmonth();
		kaoqinMonth.setYear(currentYear);
		kaoqinMonth.setMcloseflag((byte)0);
		//得到总共几个月
		int totalMonth=12-currentMonth+1;
		//所以要在数据库中插入totalMonth条记录
		int rsCount=0;
		for(int i=1 ,month=currentMonth;i<=totalMonth && month<=12;i++,month++) {
			
			kaoqinMonth.setMonth(month);
			kaoqinMonthMapper.insert(kaoqinMonth);
			rsCount++;
		}
		return rsCount;
	}

	@Override
	public int selectByCY(RsKaoqinmonth km) {
		
		return kaoqinMonthMapper.selectByCY(km);
	}

	@Override
	public int selectCompanyIdByCompanyName(String companyname) {
		
		return companyMapper.selectCompanyIDByName(companyname);
	}


	/**
	 * 根据companyId+year+month插入一条数据
	 */
	@Override
	public int start(RsKaoqinmonth km) {
		int count=0;
		km.setMcloseflag((byte)0);
		for(int i=1;i<=12;i++) {
			km.setMonth(i);
			kaoqinMonthMapper.insert(km);
			count++;
		}
		return count;
	}

	
	@Override
	public List<AskLeave> checkAsk(int year,int month) {
		AskLeave ask=new AskLeave();
		ask.setKjYear(year);
		ask.setPeriod(month);
		return askLeaveMapper.selectAskByKjYearAndPeriod(ask);
		 
	}

	@Override
	public GyPerson getPerson(int personId) {
		return personMapper.selectByPrimaryKey((long)personId);
		
	}

	@Override
	public void updateByCPYPD(int companyId, AskLeave askLeave) {
		
		CompanyIdAndAskVO caVO=new CompanyIdAndAskVO();
		caVO.setCompanyId(companyId);
		caVO.setAskLeave(askLeave);
		kaoqinMapper.updateByCPYPD(caVO);
		
	}

	@Override
	public int updateCloseFlag(int companyId,int year,int month) throws Exception {
		RsKaoqinmonth km=new RsKaoqinmonth();
		km.setComopanyid(companyId);
		km.setYear(year);
		km.setMonth(month);
		km=kaoqinMonthMapper.getKmByYMC(km);
		if(km.getMcloseflag()==1) {
			throw new Exception();
		}
		int id=km.getId();
		return kaoqinMonthMapper.updateCloseFlag(id, 1);
	}

	@Override
	public void insertPreviousYear(RsKaoqinmonth km) {
		kaoqinMonthMapper.insertPreviousYear(km);
	}

	@Override
	public int selectCloseFlag(RsKaoqinmonth km) {
		return kaoqinMonthMapper.selectCloseFlag(km);
	}

	@Override
	public RsKaoqinmonth selectByCYM(RsKaoqinmonth km) {
		return kaoqinMonthMapper.getKmByYMC(km);
	}

	@Override
	public PageInfo<RsKaoqinmonth> findKm(Integer pageSize, Integer pageNum, String searchField,
			String searchString) {
		//pageSize是从第几页开始加载，pageNum是一页显示多少条.
		String searchString2=searchString;
		if("comopanyid".equals(searchField)) {
			int companyId=companyMapper.selectCompanyIDByName(searchString);
			searchString2=String.valueOf(companyId);
		}
		
		PageHelper.startPage(pageSize, pageNum);
		List<RsKaoqinmonth> list = kaoqinMonthMapper.findKm(searchField, searchString2);
		PageInfo<RsKaoqinmonth> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}


	@Override
	public int selectLastYearHasEnd(CompanyIdAndYears years) {
		return kaoqinMonthMapper.selectLastYearHasEnd(years);
	}

	@Override
	public boolean judge(int companyId, int year) {
		//1  2017
		List<RsKaoqinmonth> list=kaoqinMonthMapper.getKmByCY(companyId, year);
		int count=0;
		for(RsKaoqinmonth km : list) {
			if(km.getMcloseflag()==1) {
				count++;
			}
		}
		if(list.size()==0 || count!=list.size()) {
			return false;
		}
		return true;
	}

	


	
	
	
	
	
	

}
